package script;
import vo.*;
import utils.*;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.By.ByLinkText;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;

public class Login {
 
	LoginPage loginpage=new LoginPage();
	webdriver_do wdo=new webdriver_do();
	
  @BeforeMethod
  public void beforeMethod() throws IOException {
	  wdo.init_webdriver();
	  
	  System.err.println("login---webdriver成功！--");
	  
	 
	  System.out.println();
  }
  
  

  
  
  @Test
  public void login() throws InterruptedException {
	  
	 wdo.wd.get(loginpage.address);
	 wdo.wd.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
	 wdo.wd.findElement(By.linkText(loginpage.login_link)).click();
	 
	 System.out.println("即将跳转到---登录页面");
	 wdo.wd.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
	 //wdo.wd.findElement(By.name(loginpage.username)).clear();
	// wdo.wd.findElement(By.name(loginpage.username)).sendKeys("jamesz2011@126.com");
	wdo.wd.findElement(By.xpath(loginpage.username1)).sendKeys("jamesz2011@126.com");
	 
	
	 //wdo.wd.findElement(By.name(loginpage.password)).clear();
	 wdo.wd.findElement(By.name(loginpage.password)).sendKeys("james9437974");
	 
	 //wdo.wd.findElement(By.id(loginpage.password)).submit();
	 
	 //wdo.wd.findElement(By.name(loginpage.login_but)).click();
	 
	 
	 WebElement but=wdo.wd.findElement(By.name(loginpage.login_but));
	 but.click();
	 
	// System.out.println("but:"+but.getAttribute("value"));
    // Assert.assertEquals(but.getAttribute("value"), "selenium");
	 System.out.println("WebElement对象");
	 
	 Thread.sleep(5000);
  }
  
  
  @AfterMethod
  public void afterMethod() throws InterruptedException, UnsupportedEncodingException {
	  
	  Thread.sleep(5000);
	  
	  wdo.wd.findElement(By.xpath("/html/body/div[1]/nav/div/ul[1]/li/a/img")).click();
	  
	//  wdo.wd.findElement(By.class("dropdown dropdown-avatar open"));
	 //wdo.wd.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
	  //img
		 //wdo.wd.findElement(By.linkText("dropdown")).click();
		 //logout
		 
		 wdo.wd.findElement(By.linkText("退出")).click();
		 
		WebElement ok=wdo.wd.findElement(By.xpath("//*[@id=\"main\"]/div[1]"));
		String ok_text= ok.getText();
		System.out.println("ok_text:"+ok_text);
		
		
		String s = "退出成功。";   
		byte[] bytes = s.getBytes(); 
	/*	String s1 = new String(bytes,"GBK"); */  
		String s1 = new String(bytes,"UTF-8");  
		
		String st = ok_text;   
		byte[] bytes1 = st.getBytes(); 
	/*	String s1 = new String(bytes,"GBK"); */  
		String s11 = new String(bytes1,"UTF-8");  
		
		System.out.println("s1:"+s1);
		System.out.println("s11:"+s11);
		//Assert.assertEquals(s11, s1);
		
		
		if (s11.equals(s1)) {
			System.out.println("找到---退出成功标志");
			ok.click();
			Thread.sleep(10000);
			  wdo.wd.quit();
			  System.out.println("ok\n");
			  System.out.println("测试完成");
		}
		else {
			  System.out.println("error\n");
			  System.out.println("测试error");
		}
		
	 
	  
	  
  }

}
